<?php
/**
 * Created by PhpStorm.
 * User: sunwu
 * Date: 2018/2/8
 * Time: 15:31
 */

namespace app\home\controller;

use app\home\def\Err;
use app\home\def\Helper;
use think\Controller;
use app\home\model\User as userModel;
use think\Request;
use think\Url;

class Login extends Controller
{

    private $userModel;
    private $err;

    public function __construct()
    {
        parent::__construct();
        $this->userModel = new userModel();
        $this->err = new Err();
        Url::root('/index.php');
    }

    public function login()
    {
        if (!empty(session('name'))) $this->success('你已经登陆了', 'user/home');
        return view();
    }

    public function loginApi()
    {
        /*
         * email:1228746736@qq.com
            password:1
            vercode:tcqy
         */
        if (session('uid')) return $this->err->info(0);
        if (!Helper::checkVercode(input('post.vercode'), 'login')) return $this->err->info(2);
        $email = input('post.email');
        $pwd = input('post.password');
        $userInfo = $this->userModel->where('Email', $email)->where('password', $pwd)->find();
        if (!$userInfo) {
            return $this->err->info(10002);
        } else {
            session('uid', $userInfo['PK_UserGuid']);
            session('name', $userInfo['Name']);
            session('ucover', $userInfo['Cover']);
            session('ulevel', $userInfo['Level']);
            session('upwd', $userInfo['password']);
            session('ureports', $userInfo['reports']);
            return $this->err->info(0);
        }

    }


    public function logout()
    {
        session('uid', null);
        session('name', null);
        session('ucover', null);
        session('ulevel', null);
        session('upwd', null);
        session('ureports', null);
        $this->success('退出成功', 'index/index');
    }

    public function reg()
    {
        return view();
    }

    public function regApi()
    {
        /*
         * username:s
            Email:w@qq.com
            password:1
            repassword:1
            vercode:mzaa
         */
        $data = input('post.');
        $data['Name'] = input('post.username');
        $data['PK_UserGuid'] = get_guid();
        $data['Status'] = '已激活';
        //激活还没有做
        return $this->err->resUpdata($this->userModel->allowField(true)->save($data));

    }

    public function forget()
    {
        return view();
    }

    /**
     * 找回密码邮件
     */
    public function resetMail($to = '')
    {
        if (!$this->checkEmailIsExist($to)) {
            return $this->err->info(10003);
        }
        $code = email_to_code($to);
        $body = '<h1>您的重置密码验证码为</h1>' . $code;
        $title = '重置密码';
        $sres = Helper::sendMail($to, $title, $body);
        if ($sres['errno'] === 0) {
            return $this->err->info(0);
        } else {
            return $this->err->info(-1);
        }

    }

    public function resetApi()
    {
        $email = input('post.email');
        $code = input('post.code');
        $pass = input('post.pass');
        $mycode = email_to_code($email);
        if ($mycode !== $code) {
            return $this->err->info(2);
        }
        return $this->err->resUpdata($this->userModel->save(['password' => $pass], ['Email' => $email]));

    }

    private function checkEmailIsExist($email = '')
    {
        $res = $this->userModel->where('Email', $email)->value('Email');
        if ($res) return $res;
        return false;
    }

    /**
     * 重设密码页面
     */
    public function reset()
    {
        return view();
    }


    public function checkVercode($code, $id = '')
    {
        if (Helper::checkVercode($code, $id) === true) {
            return $this->err->info(0);
        } else {
            return $this->err->info(2);
        }
    }

}